package com.rabbit.controller;

import com.github.pagehelper.PageInfo;
import com.rabbit.annotation.Log;
import com.rabbit.annotation.LoginUser;
import com.rabbit.constant.OperType;
import com.rabbit.db.entity.User;
import com.rabbit.exception.BusinessException;
import com.rabbit.service.RoleService;
import com.rabbit.util.ResultGenUtil;
import com.rabbit.validate.base.Delete;
import com.rabbit.validate.base.Enable;
import com.rabbit.validate.base.Insert;
import com.rabbit.validate.base.Update;
import com.rabbit.vo.ResultVO;
import com.rabbit.vo.req.RoleReq;
import com.rabbit.vo.res.RoleRes;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;

/**
 * <h1>角色管理</h1>
 *
 * @author zjw
 * @date 2022/1/18 9:20
 */
@RestController
@RequestMapping("/role")
public class RoleController {

    @Resource
    private RoleService roleService;

    /**
     * 角色列表
     */
    @PostMapping("/list")
    public ResultVO<Object> list(@RequestBody RoleReq roleReq) {
        PageInfo<RoleRes> pageInfo = roleService.queryRoleList(roleReq);
        return ResultGenUtil.genSuccess(pageInfo.getList(), pageInfo.getTotal());
    }

    /**
     * 删除角色
     */
    @PostMapping("/delete")
    @Log(name = "角色管理", type = OperType.DELETE)
    public ResultVO<Object> deleteRole(@Validated(Delete.class) @RequestBody RoleReq roleReq) throws BusinessException {
        roleService.checkIsAdmin(roleReq);
        roleService.checkRoleUsed(roleReq);
        roleService.deleteRole(roleReq);
        return ResultGenUtil.genSuccess();
    }

    /**
     * 调整角色状态
     */
    @PostMapping("/changeEnabled")
    @Log(name = "角色管理", type = OperType.UPDATE)
    public ResultVO<Object> changeEnabled(@LoginUser User loginUser, @Validated(Enable.class) @RequestBody RoleReq roleReq) throws BusinessException {
        roleService.checkIsAdmin(roleReq);
        roleReq.setOperatorUserName(loginUser.getUserName());
        roleService.updateRoleEnabled(roleReq);
        return ResultGenUtil.genSuccess();
    }

    /**
     * 查询指定角色
     */
    @GetMapping("/queryRoleById")
    public ResultVO<Object> queryRoleById(@RequestParam Long roleId) {
        return ResultGenUtil.genSuccess(roleService.queryRoleById(roleId));
    }

    /**
     * 新增角色
     */
    @PostMapping("/add")
    @Log(name = "角色管理", type = OperType.INSERT)
    public ResultVO<Object> addRole(@LoginUser User loginUser, @Validated(Insert.class) @RequestBody RoleReq roleReq) throws BusinessException {
        roleService.checkUnique(roleReq, 1);
        roleReq.setOperatorUserName(loginUser.getUserName());
        roleService.addRole(roleReq);
        return ResultGenUtil.genSuccess();
    }

    /**
     * 修改角色
     */
    @PostMapping("/update")
    @Log(name = "角色管理", type = OperType.UPDATE)
    public ResultVO<Object> updateRole(@LoginUser User loginUser, @Validated(Update.class) @RequestBody RoleReq roleReq) throws BusinessException {
        roleService.checkIsAdmin(roleReq);
        roleService.checkUnique(roleReq, 2);
        roleReq.setOperatorUserName(loginUser.getUserName());
        roleService.updateRole(roleReq);
        return ResultGenUtil.genSuccess();
    }

}
